﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using H2.NET.Business;

namespace H2.NET
{
    public abstract class TaskControl : System.Web.UI.UserControl
    {
        #region 属性 Public
        public TaskAPI TaskAPI
        {
            get 
            {
                return (Page as TaskPage).TaskAPI;
            }
        }

        public t_User UserAPI
        {
            get
            {
                return (Page as TaskPage).UserAPI;
            }
        }

        public TaskDB DBAPI
        {
            get
            {
                return (Page as TaskPage).DBAPI ;
            }
        }

        public TaskStore StoreAPI
        {
            get
            {
                return (Page as TaskPage).StoreAPI;
            }
        }

        public string TaskID
        {
            get
            {
                return (Page as TaskPage).TaskID;
            }
        }

        public string Process
        {
            get
            {
                return (Page as TaskPage).Process;
            }
        }

        public string Incident
        {
            get
            {
                return (Page as TaskPage).Incident;
            }
        }

        public string Step
        {
            get
            {
                return (Page as TaskPage).Step;
            }
        }

        public string T
        {
            get
            {
                return (Page as TaskPage).T;
            }
        }

        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }

        #endregion

        #region 重载 OnPreRender
        //protected override void OnPreRender(EventArgs e)
        //{
        //    if (!IsLoaded && Page is TaskPage)
        //    {
        //        TaskPage TP = (TaskPage)Page;

        //        TaskID   = TP.TaskID;
        //        Process  = TP.Process;
        //        Incident = TP.Incident;
        //        Step     = TP.Step;
        //        T        = TP.T;

        //        IsLoaded = true;
        //    }

        //    base.OnPreRender(e);
        //}

        //protected override void OnLoad(EventArgs e)
        //{
        //    if (!IsLoaded && Page is TaskPage)
        //    {
        //        TaskPage TP = (TaskPage)Page;

        //        TaskID      = TP.TaskID;
        //        Process     = TP.Process;
        //        Incident    = TP.Incident;
        //        Step        = TP.Step;
        //        T           = TP.T;

        //        IsLoaded = true;
        //    }

        //    base.OnLoad(e);
        //}
        #endregion

        #region 方法 SetEnable(设置控件禁启用)
        //设置控件禁启用
        /// <summary>
        /// 
        /// </summary>
        /// <param name="IsEnable"></param>
        /// <param name="WebControl"></param>
        protected void SetEnable(bool IsEnable, params WebControl[] WebControl)
        {
            foreach (WebControl ctl in WebControl)
            {
                ctl.Enabled = IsEnable;
            }
        }
        #endregion

        #region 方法 SetVisible(设置控件显示隐藏)
        protected void SetVisible(bool IsEnable, params WebControl[] WebControl)
        {
            foreach (WebControl wc in WebControl)
            {
                wc.Visible = IsEnable;
                wc.Attributes["v"] = IsEnable ? "1" : "0";
            }
        }
        #endregion

        #region 方法 SetEmptyValue(清空控件值)
        protected void SetEmptyValue(params WebControl[] WebControl)
        {
            foreach (WebControl ctl in WebControl)
            {
                switch (ctl.GetType().Name)
                {
                    case "TextBox":
                        ((TextBox)ctl).Text = "";
                        break;
                    case "DropDownList":
                        ((DropDownList)ctl).SelectedIndex = -1;
                        break;
                    case "Label":
                        ((Label)ctl).Text = "";
                        break;
                    case "CheckBox":
                        ((CheckBox)ctl).Checked = false;
                        break;
                    case "CheckBoxList":
                        CheckBoxList cbls = (CheckBoxList)ctl;
                        for (int i = 0; i < cbls.Items.Count; i++)
                        {
                            cbls.Items[i].Selected = false;
                        }
                        break;
                    case "Calendar":
                        ((TextBox)ctl).Text = "";
                        break;
                    case "Dialog":
                        ((TextBox)ctl).Text = "";
                        break;
                }
            }
        }
        #endregion

        #region 方法 SetForeColor(设置前景颜色)
        protected void SetForeColor(Color Color, params WebControl[] WebControl)
        {
            foreach (WebControl ctl in WebControl)
            {
                ctl.ForeColor = Color;
            }
        }
        #endregion

        #region 方法 Validation 数据验证检查
        public static List<string> Validation(List<Regular> listRegular)
        {
            List<string> strlist = new List<string>();

            foreach (Regular reg in listRegular)
            {
                if (reg.Match == RegularExp.NotEmpty)
                {
                    if(string.IsNullOrEmpty(reg.Value.ToString()))
                    {
                        strlist.Add(reg.Message);
                    }
                }
                else
                {
                    System.Text.RegularExpressions.Regex strReg = new System.Text.RegularExpressions.Regex(reg.Match);
                    if (!strReg.IsMatch(reg.Value.ToString()))
                        strlist.Add(reg.Message);
                }

            }
            return strlist;
        }

        public static List<string> Validation(string regstr, List<Regular> listRegular)
        {
            List<string> strlist = new List<string>();

            foreach (Regular reg in listRegular)
            {
                if (regstr == RegularExp.NotEmpty)
                {
                    if (string.IsNullOrEmpty(reg.Value.ToString()))
                    {
                        strlist.Add(reg.Message);
                    }
                }
                else
                {
                    System.Text.RegularExpressions.Regex strReg = new System.Text.RegularExpressions.Regex(regstr);
                    if (!strReg.IsMatch(reg.Value.ToString()))
                        strlist.Add(reg.Message);
                }

            }
            return strlist;
        }

        public static List<string> Validation(string regstr, params Regular[] regs)
        {
            List<string> strlist = new List<string>();
            foreach (Regular reg in regs)
            {
                if (regstr == RegularExp.NotEmpty)
                {
                    if (string.IsNullOrEmpty(reg.Value.ToString()))
                    {
                        strlist.Add(reg.Message);
                    }
                }
                else
                {
                    System.Text.RegularExpressions.Regex strReg = new System.Text.RegularExpressions.Regex(regstr);
                    if (!strReg.IsMatch(reg.Value.ToString()))
                        strlist.Add(reg.Message);
                }
            }
            return strlist;
        }
        #endregion

        #region 方法 akzo 是否验证检查
        public static void akzo_RegularCheck(ref List<Regular> regs, string match, WebControl wc, object obj, string message)
        {
            if (wc.ForeColor == Color.Blue && wc.Attributes["v"] == "1")
            {
                regs.Add(new Regular(match, obj, message));
            }
        }
        #endregion
    }
}